//
//  DBRefreshGifHeader.m
//  DuoBao
//
//  Created by arons on 16/5/24.
//  Copyright © 2016年 aron. All rights reserved.
//

#import "DBRefreshGifHeader.h"
#import "UIColor+Util.h"
#import "UIViewAdditions.h"

@interface DBRefreshGifHeader ()
{
    __unsafe_unretained UIImageView *_gifView;
    __unsafe_unretained UIImageView *_pullingView;
}
/** 所有状态对应的动画图片 */
@property (strong, nonatomic) NSMutableDictionary *stateImages;
/** 所有状态对应的动画时间 */
@property (strong, nonatomic) NSMutableDictionary *stateDurations;

@end

@implementation DBRefreshGifHeader

#pragma mark - 懒加载
- (UIImageView *)gifView
{
    if (!_gifView) {
        UIImageView *gifView = [[UIImageView alloc] init];
        gifView.backgroundColor = [UIColor themeViewBackgroundColor];
        [self addSubview:gifView];
        _gifView = gifView;
        
        // 添加下拉状态的UIImageView
        UIImageView* pullingView = [[UIImageView alloc] init];
        pullingView.tag = 10235;
        pullingView.backgroundColor = [UIColor themeViewBackgroundColor];
        pullingView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:pullingView];
        _pullingView = pullingView;

    }
    return _gifView;
}

- (NSMutableDictionary *)stateImages
{
    if (!_stateImages) {
        self.stateImages = [NSMutableDictionary dictionary];
    }
    return _stateImages;
}

- (NSMutableDictionary *)stateDurations
{
    if (!_stateDurations) {
        self.stateDurations = [NSMutableDictionary dictionary];
    }
    return _stateDurations;
}

#pragma mark - 公共方法
- (void)setImages:(NSArray *)images duration:(NSTimeInterval)duration forState:(MJRefreshState)state
{
    if (images == nil) return;
    
    self.stateImages[@(state)] = images;
    self.stateDurations[@(state)] = @(duration);
    
    /* 根据图片设置控件的高度 */
    UIImage *image = [images firstObject];
    if (image.size.height > self.mj_h) {
        self.mj_h = image.size.height;
    }
}

- (void)setImages:(NSArray *)images forState:(MJRefreshState)state
{
    [self setImages:images duration:images.count * 0.1 forState:state];
}

#pragma mark - 实现父类的方法
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    
    // 更新下阿状态图的约束
    CGFloat constantY = self.height*(MIN(1.0f, MAX(0, 1.0f-pullingPercent)));
    CGRect newF = CGRectMake(0, constantY, self.width, self.height - constantY);
    _pullingView.frame = newF;
    // _topConstraint.constant = constant;
     // NSLog(@" pullingPercent %.2f  constantY: %f  newF:%@", pullingPercent, constantY, NSStringFromCGRect(newF));

    NSArray *images = self.stateImages[@(MJRefreshStateIdle)];
    if (self.state != MJRefreshStateIdle || images.count == 0) return;
    // 停止动画
    [self.gifView stopAnimating];
    
    // 设置当前需要显示的图片
    UIImage* img = [images lastObject];
    [self bringSubviewToFront:_pullingView];
    _pullingView.hidden = NO;
    _pullingView.image = img;
    
}

- (void)placeSubviews
{
    [super placeSubviews];

    self.stateLabel.hidden = YES;
    self.lastUpdatedTimeLabel.hidden = YES;
    
    // 设置动画的ImageView
    if (self.gifView.constraints.count) return;
    
    // _pullingView.frame = self.bounds;
    self.gifView.frame = self.bounds;
    if (self.stateLabel.hidden && self.lastUpdatedTimeLabel.hidden) {
        self.gifView.contentMode = UIViewContentModeCenter;
    } else {
        self.gifView.contentMode = UIViewContentModeRight;
        self.gifView.mj_w = self.mj_w * 0.5 - 90;
    }
    
}

- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState
    
    // 根据状态做事情
    _pullingView.hidden = NO;

    if (state == MJRefreshStatePulling || state == MJRefreshStateRefreshing) {
        NSArray *images = self.stateImages[@(state)];
        _pullingView.hidden = YES;
        
        if (images.count == 0) return;
        
        [self.gifView stopAnimating];
        if (images.count == 1) { // 单张图片
            self.gifView.image = [images lastObject];
        } else { // 多张图片
            self.gifView.animationImages = images;
            self.gifView.animationDuration = [self.stateDurations[@(state)] doubleValue];
            [self.gifView startAnimating];
        }
    } else if (state == MJRefreshStateIdle) {
        
//        NSArray* subViews = [self subviews];
//        BMLog(@"subViews = %@", subViews);
        
        [self.gifView stopAnimating];
        _pullingView.hidden = NO;
        [self bringSubviewToFront:_pullingView];
        
        NSArray *images = self.stateImages[@(MJRefreshStateIdle)];
        // 停止动画
        [self.gifView stopAnimating];
        // 设置当前需要显示的图片
        UIImage* img = [images lastObject];
        self.gifView.image = img;
    }
}

@end
